<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/operateTag" prefix="o"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>资源管理</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<jsp:include page="/page/head.jsp"></jsp:include>
	<script type="text/javascript">
		var path = "<%=path%>";
		
		$(document).ready(function(){
			
		    $('#mgrTable').treegrid(); 
		});
		
		var url;
		
		function add(){
			var selected = $('#mgrTable').treegrid('getSelected');
			if(selected){
				$('#dlg').dialog('open').dialog('setTitle','新增资源');
				$('#fm').form('clear');
				$('#permissionId').combotree('enable');
				url = path +"/resource/save.do?resourceId="+selected.id;
			}else{
				alertBox("请选择父资源节点！");
			}
		}
		
		function edit(){
			var selected = $('#mgrTable').treegrid('getSelected');
			if(selected){
				$('#dlg').dialog('open').dialog('setTitle','编辑资源');
				$('#fm').form('load',selected);
				$('#permissionId').combotree('setValue', selected.permissionId);
				$('#permissionId').combotree('disable');
				url = path +"/resource/update.do?resourceId="+selected.id;
			}else{
				alertBox("请选择需要编辑的资源！");
			}
		}
		
		function remove(){
			var selected = $('#mgrTable').treegrid('getSelected');
			if(selected){
				var submit = function (v, h, f) {
				    if (v == 'ok'){
				    	url = path +"/resource/delete.do?resourceId="+selected.id+"&t="+new Date().getTime();
						$.ajax({
						  url: url,
						  success: function(data) {
							   if(data == "1")
							   {
									$('#mgrTable').treegrid('reload');
									alertBox("操作成功！");
							   } else {
									alertBox("操作失败！");
							   }
						  }
						});
				    }
				    	
				    return true; //close
				};
				
				$.jBox.confirm("确定要删除吗？", "提示", submit);
				
			}else{
				alertBox("请选择需要删除的资源！");
			}
		}
		
		function refresh(){
			$('#mgrTable').treegrid('reload');
		}
		
		function save(){
			if($('#fm').form('validate')){
				var data = "&permissionId="+$('#permissionId').combotree('getValue');
				data += "&name="+escape(escape($('#name').val()));
				data += "&url="+$('#url').val();
				data += "&layerOrder="+$('#layerOrder').val();
				$.ajax({
				  url: url,
				  data:data,
				  success: function(data) {
					   if(data == "1")
					   {
						    $('#dlg').dialog('close');	
							$('#mgrTable').treegrid('reload');
							alertBox("操作成功！");
					   } else {
							alertBox("操作失败！");
					   }
				  }
				});
				
			}
		}
		
	</script>

  </head>
  
  <body>
    <div class="right">
		<h1><b>资源管理</b><span><img src="<%=basePath %>images/right_h1bg2.jpg" width="3" height="29" /></span></h1>
		<div class="ri_main">
			<div id="toolbar">
				<a href="javascript:void(0);" onclick="add()" class="easyui-linkbutton" iconCls="icon-add">新增</a>
				<a href="javascript:void(0);" onclick="edit()" class="easyui-linkbutton" iconCls="icon-edit">编辑</a>
				<a href="javascript:void(0);" onclick="remove()" class="easyui-linkbutton" iconCls="icon-remove">删除</a>
				<a href="javascript:void(0);" onclick="refresh()" class="easyui-linkbutton" iconCls="icon-reload">刷新</a>
			</div>
		 	<table id="mgrTable" toolbar="#toolbar" idField="id" treeField="name" 
		 		url="resource/list.do" singleSelect="true" rownumbers="true" animate="true">
		 		<thead>
					<tr>
						<th field="name" width="200" frozen="true">资源名称</th>
						<th field="url" width="200">资源路径</th>
						<th field="layerOrder" width="200">排序编号</th>
					</tr>
				</thead>
		 	</table>  
		</div>
	</div>
	
	<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
		closed="true" buttons="#dlg-buttons">
		<form id="fm" method="post" novalidate >
			<div class="fitem">
				<label>所属权限</label>
				<input name="permissionId" id="permissionId" class="easyui-combotree"  url="permission/tree.do" required="true" style="width:200px;">
			</div>
			<div class="fitem">
				<label>资源名称</label>
				<input id="name" name="name" class="easyui-validatebox" required="true" style="width:200px;">
			</div>
			<div class="fitem">
				<label>资源路径</label>
				<input id="url" name="url" class="easyui-validatebox" style="width:200px;">
			</div>
			<div class="fitem">
				<label>排序编号</label>
				<input id="layerOrder" name="layerOrder" class="easyui-validatebox" style="width:200px;">
			</div>
		</form>
	</div>
	<div id="dlg-buttons">
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">保存</a>
		<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
	</div>
  </body>
</html>
